﻿using ReflectionStudio.Classes;
using ReflectionStudio.Core.Events;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    //public partial class MainWindow : RibbonWindow, IIntexReptServBLCallback
    public partial class MetroWindow : Window
    {
 
        public string LayoutFile
        {
            get { return app.LayoutFile; }
        }
        #region ==IsLogOn===========
        public static readonly DependencyProperty IsLogOnProperty = DependencyProperty.Register("IsLogOn",
            typeof(bool), typeof(MetroWindow),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsLogOnChanged));
        public bool IsLogOn
        {
            get
            {
                return (bool)GetValue(IsLogOnProperty);
            }
            set
            {
                SetValue(IsLogOnProperty, value);
                vm.IsLogOn = value;
             }
        }
        private static void OnIsLogOnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MetroWindow;
            if (src != null)
            {
                src.IsLogOn = (bool)e.NewValue;
            }
        }
        #endregion
  
        ApplicationBase app { get { return ApplicationBase.Instance; } }
        string AgentID { get { return app.proxy.AgentID; } }
        public string RUNID = "";
        MetroViewModel vm;
        private Process _process;
        UIMENU menu
        {
            get
            {
                return (UIMENU)this.FindName("MainMenu");
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public MetroWindow()
        {
            try
            {
                app.Load();
                app.IsSTA = true;
                app.PrepareMulLang("INTEXREPORTCLIENT");
                if (!app.Connected)
                {
                    throw new Exception("请检查网络或重试");
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            try
            {
                RUNID = "ROOT";
                vm = new MetroViewModel();
                this.DataContext = vm;
                InitializeComponent();
                menu.ORDERNUM = app.User;
                IsLogOn = false;
                Tracer.Verbose("主程序启动", "开始");
                DocumentFactory.Instance.Initialize(MainTab);
                //catch events
                EventDispatcher.Instance.OnStatusChange += new EventHandler<StatusEventArgs>(this.MainStatusBar.OnStatusChange);
                //forward also all changes to the log toolbox
                ((MetroViewModel)vm).menu.LoadAssembly(Assembly.GetExecutingAssembly());
                //MainMenu.SetUpUserMenu(IsTranslate); 
                DisplayHomeDocument();
                //ApplicationBase.Instance.StartListen();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                Tracer.Verbose("主程序启动", "结束");
            }
        }

       
        

        private void DisplayHomeDocument()
        {
            string doctype = dicParamater["DocType"].ToString();
            menu.ActivateReportByDocType(doctype);
        }
 
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            app.Quit();
            base.OnClosing(e);
            if (_process != null)
            {
                _process.Refresh();
                _process.Close();
            }
        }

        private void SetMasterCommand(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            ImageBrush brush = btn.Background as ImageBrush;
            MessageInfo msg = new MessageInfo();
            if (app.IsMaster)
            {
                app.IsMaster = false;
                brush.RelativeTransform = null;
            }
            else
            {
                app.IsMaster = true;
                Helpers.Helper.RotateImage(btn);
            }
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHAGELVL";
            msg.ACTION = "IsMaster=true";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        private void SetLogCommand(object sender, RoutedEventArgs e)
        {
            vm.IsShowLog = !vm.IsShowLog;
        }
        private void SetLogOnCommand(object sender, RoutedEventArgs e)
        {
            IsLogOn = false;

        }
        private void CloseWindowCommand(object sender, RoutedEventArgs e)
        {
            this.Close();
            System.Environment.Exit(0);
        }

        private void DBQueryCommand(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.DB02(this.RUNID);
        }
        private void TSModeCommand(object sender, RoutedEventArgs e)
        {

            vm.IsTS = false;
            app.AppMode = "TMODE"; 

            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHGMODE";
            msg.ACTION = "CHGMODE";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        private void TMModeCommand(object sender, RoutedEventArgs e)
        {
            app.AppMode = "TISSUE";
            vm.IsTS = true;
            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHGMODE";
            msg.ACTION = "CHGMODE";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }
    }
}
